﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>OnlineDict 选项页</title>
    <link rel="stylesheet" type="text/css" href="options.css">
    <link rel="stylesheet" type="text/css" href="jqac.css">
<script type="text/javascript" src="newwords.js"></script>
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript" src="remember.js"></script>
<script type="text/javascript" src="date.js"></script>
<script> 

//从本地加载文件
function LoadLocalPage(localFile,funSuccess,funError)
{
    $.ajax({
      url:localFile,
      type: 'GET',
      success: function(data) {
         funSuccess(data);
      },
      error: function(XMLHttpRequest, textStatus, errorThrown){
         funError(textStatus);
      }
    });
}
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}
var now=new Date().getTime();
var last=new Date().getTime();
function timeTest(info,cnt)
{
    now=new Date().getTime();
    console.log(info+", "+cnt+":"+(now-last));
    last=now;
}
function ImportData(txtData){
    var words=[];
    var d=txtData;
    //sleep(50*1000);
    //db.ImportVocabulary("ddddfd","abd");
    db.getLeftWordsLength(function(length){
        var allcnt=0;
        if(length==0)
            return;
        if(length>0) //截取已经写入数据库的内容
        {
            d=d.slice(d.length-length);
        }
        else
        {
            db.updateLeftWordsLength(String(d.length));
        }
        var ws=[];
        var infos=[];
        var curindex=0;
        function l()
        {
            //console.log("allcnt="+allcnt+",curindex="+curindex);
        }
        function f()
        {
            var cnt=0;
            while(cnt<1000)
            {
            //    l();
                cnt++;
                allcnt++;
                var i=d.indexOf("\n",curindex);
                if(i==-1)
                    break;
                var w=d.substring(curindex,i);
                if(w.length==0 || w.length>36) 
                {
                    console.log("too long word:"+w);
                    break;
                }
                curindex=i+1;
                if(d.indexOf("!$$-^",curindex)!=curindex)
                {
                    console.log("want a tag at start, but not found");
                    break;
                }
                curindex+=5;
                i=d.indexOf("!$$-^",curindex);
                var info=d.substring(curindex,i);
                curindex=i+5;
                ws.push(w);
                words.push(w);
                infos.push(info);
                
            }
            l();
            if(d.length>0)
            {
                waitforwsempty();
            }
        }

        function waitforwsempty()
        {
            if(ws.length==0)
            {
                d=d.slice(curindex);
                curindex=0;
                setTimeout(f,50);
            }
            else
            {
                db.ImportVocabularyByWords(ws,infos,d.length-curindex,function(result){
                    ws=[];
                    infos=[];
                    waitforwsempty();
                });
            }
        }
        f();
    });
    return words;
}
function checkUpdateVocabulary()
{
    db.getLeftWordsLength(function(length){
        if(length==0)
        {
            getid("span_info").innerHTML = "";
            //已经有数据，不用导入了
            return;
        }
		 getid("span_info").innerHTML = "";
		/*
        getid("span_info").innerHTML = "正在导入词汇表……";
        LoadLocalPage('/vocabulary.txt',function (data){
            var words = ImportData(data);
            getid("span_info").innerHTML = words.length + "个词汇已导入";
        });
		*/
    });
}

function getSuguest(str,cont){ 
    db.GetVocabulary(str + "%", function(words){
        var res = [];
        for(var i = 0;i<words.length;i++)
        {
            res.push({id:words[i].id,value:words[i].word});
        }
        cont(res);
    });
}
function onSuguestFinished(obj){ 
  WebTranslate();
}
//设置默认按钮
function   SubmitKeyClick(button)     
{       
    if   (event.keyCode   ==   13)     
    {   
        document.getElementById(button).focus();           
        document.getElementById(button).click();   
    }   
}   
$(document).ready(function(){
	ShowPage(currentpage);
	UpdateWordCounts();
    //window.setTimeout(checkUpdateVocabulary,10*1000);
    checkUpdateVocabulary();
    
    $('#input_word').autocomplete({minchars:2,height:500,delay:0,callback:onSuguestFinished,ajax_get:getSuguest});
 });
</script>
</head>
<body>
	<div id="debug"></div>
    <div id="main_container">
        <table width="98%" valign="top">
            <tr>
                <td>
                    <h1>
                        <img src="logo.png" width="30" />OnlineDictPlus 单词学习助手</h1>
                </td>
                <td align="right">
                    
                        <a href="./remember.htm" target="_blank">首页</a> | 
						<a href="./options.htm" target="_blank">选项</a> | 
						<a href="http://code.google.com/p/onlinedictplus/" target="_blank">OnlineDictPLus主页</a> 
							<br/><br/><font color=green><span id='span_newwords'>新单词：0个</span>，<span id='span_rememberwords'>记住了：0个</span><br/><span id='span_info'>导入词汇表……</span></font>
                </td>
            </tr>
            <tr><td>
<input id="input_word" onkeydown="SubmitKeyClick('btn_websearch')"/>
<input type="button" value="网络查词" onclick="WebTranslate();" id="btn_websearch"/>
</td><td></td></tr>
</table>
<div id="div_webTranslate"></div>
        <table cellpadding="5" class="settings-table">
            <tr>
                <td valign="top" width="75%">
                    <table width="100%">
                        <tr>
                            <td>
                                排序:
                                <input name="rdOrderKey" type="radio" value="translateCount" checked="true" onclick="SetOrder()">取词次数
                                <input name="rdOrderKey" type="radio" value="addTime" onclick="SetOrder()">取词时间
                                <input name="rdOrderKey" type="radio" value="word" onclick="SetOrder()">单词
								&nbsp;&nbsp;&nbsp;&nbsp;
								<input name="rdOrder" type="radio" checked="true" value="desc" onclick="SetOrder()">降序
								<input name="rdOrder" type="radio" value="asc" onclick="SetOrder()">升序
                            </td>
							<td>
							<select id="filterid" name="filter" onchange="filterWords();">
								<option value="all"> 全部时间
								<option value="today"> 今天
								<option value="threedays"> 三天内
								<option value="week">一周内
								<option value="twoweeks">两周内
							</select>
							</td>
                            <td align="right">
                                <input type="button" value="上一页" onclick="ShowPrevPage()" /><input type="button" value="下一页"
                                    onclick="ShowNextPage()" />
                            </td>
                            <td align="left">
                                <div id="itemcount" align="left">
                                    共N条</div>
                            </td>
                        </tr>
                    </table>
                    <div id="div_words">
                    </div>
                </td>
            </tr>
        </table>
</body>
<script src="lookup.js" type="text/javascript"></script> 
<script type="text/javascript" src="jquery.autocomplete-1.4.2.js"></script>
</html>
